//
//  BaseViewController.m
//  TextToImg
//
//  Created by Garenge on 2019/M/29.
//  Copyright © 2019 鹏鹏. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (CGFloat)naviBarBottomMargin {
    UINavigationBar *naviBar = self.navigationController.navigationBar;
    CGFloat bottomMargin = 0;
    if (self.navigationController && self.navigationController.navigationBar) {
        CGRect convertRect = [[UIApplication sharedApplication].keyWindow convertRect:naviBar.frame fromView:naviBar.superview];
        //        bottomMargin = naviBar.frame.size.height + naviBar.frame.origin.y;
        bottomMargin = convertRect.size.height + convertRect.origin.y;
    }
    return bottomMargin;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}

#pragma mark alert
- (void)AlertTarget:(id)target title:(NSString *)title message:(NSString *)message sureTitle:(NSString *)sureTitle sureAction:(void(^)(void))sureAction cancleTitle:(NSString *)cancleTitle cancleAction:(void(^)(void))cancleAction {
    
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // sureAct
    UIAlertAction *sureAct = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureAction();
    }];
    
    // cancleAct
    UIAlertAction *cancleAct = [UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        cancleAction();
    }];
    
    [alertView addAction:sureAct];
    [alertView addAction:cancleAct];
    
    [target presentViewController:alertView animated:NO completion:nil];
    
}

- (void)AlertTarget:(id)target title:(NSString *)title message:(NSString *)message sureTitle:(NSString *)sureTitle sureAction:(void (^)(void))sureAction {
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // sureAct
    UIAlertAction *sureAct = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        sureAction();
    }];
    
    [alertView addAction:sureAct];
    
    [target presentViewController:alertView animated:NO completion:nil];
}

#pragma mark 执行自定义方法
- (void)performSelfFuncWithString:(NSString *)funcString {
    if ([self respondsToSelector:NSSelectorFromString(funcString)]) {
        
        SEL selector = NSSelectorFromString(funcString);
        IMP imp = [self methodForSelector:selector];
        void (*func)(id, SEL) = (void *)imp;
        func(self, selector);
    }
}

@end
